package com.model.dao.base.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.Source;
import org.springframework.stereotype.Repository;

import com.model.dao.base.BaseDao;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
@Repository("baseDao")
public class BaseDaoImpl<T>   implements BaseDao<T> {

	protected Session session;
	private Class<T> clazz;
	@Source
	protected SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	@SuppressWarnings("unchecked")
	public BaseDaoImpl() {
		// 当前对象的直接超类的 Type
		Type genericSuperclass = getClass().getGenericSuperclass();
		if (genericSuperclass instanceof ParameterizedType) {
			// 参数化类型
			ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
			// 返回表示此类型实际类型参数的 Type 对象的数组
			Type[] actualTypeArguments = parameterizedType
					.getActualTypeArguments();
			this.clazz = (Class<T>) actualTypeArguments[0];
		
		} else {
			this.clazz = (Class<T>) genericSuperclass;
		}
	
	}

	public Class<T> getCls() {
		return clazz;
	}
	
	@Override
	public void save(T t) {
		getSession().save(t);
		flush();
		closeSession();
	}

	@Override
	public void update(T t) {
		getSession().update(t);
		flush();
		closeSession();
	}

	@Override
	public void delete(T t) {
		getSession().delete(t);
		flush();
		closeSession();
	}

	@Override
	public List<T> QueryList(T t) {
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public T QueryById(int id) {
		System.out.println(getCls());
		return  (T) getSession().get(getCls(), id);
	}

	@Override
	public List<T> QueryBySqlList(String sql, T t) {
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> QueryByHqlList(String hql, Object[] args) {
		Query q = this.getSession().createQuery(hql);
			if (args != null) {
				for (int i = 0; i < args.length; i++) {
					q.setParameter(i, args[i]);
				}
			}
			List<T> list  = (List<T>) q.list();	
			flush();
			closeSession();
           return list;
	}

	@Override
	public int getAllRowCount(String hql, Object[] args) {
		return QueryByHqlList(hql, args).size();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> queryForPage(int offset, int length, String hql) {
		Query q = this.getSession().createQuery(hql);
		q.setFirstResult(offset); // 当前页数
		q.setMaxResults(length); // 每页显示的记录数
				List<T> list  = (List<T>) q.list();	
				flush();
				closeSession();
	return list;
	}

	@Override
	public void flush() {
		if(session!=null){
			session.flush();
		}
		
	}

	/**
	 * 获取session
	 * 
	 * @return
	 */
	public Session getSession() {
	  if(session==null){
		  return sessionFactory.openSession();
	  }else{
		  return session;
	  }
	}

	/**
	 * 关闭session
	 */
	public void closeSession() {
		 if(session!=null){
		  session.close();
		}
	}

}



